<?php
class admin_model extends Model
{
      function admin_model()
	  {
          parent::Model();
      }
      
      function getAllAdmin($num, $offset)
	  {
          $query = $this->db->get('admin', $num, $offset);
          return $query->result();  
      }
      
      function getNumAdmin(){
          $query = $this->db->get('admin');
          return $query->num_rows();
      }
      
      function getAdmin()
	  {
          $id = $this->uri->segment(3);
          $this->db->where('AdminID',$id);
          $query = $this->db->get('admin');
          return $query->row();
      }
      
      function saveAdmin(){
          $AdminID = (int)$this->uri->segment(3);
          if($AdminID!=0){
              $data_pass = array(
                'LoginName' => $this->input->post('LoginName'),
                'Passwd' => md5($this->input->post('Passwd'))
              );
              $data_no_pass = array(
                'LoginName' => $this->input->post('LoginName')
              );
              if($this->input->post('Passwd')!=''){
                  $data = $data_pass;
              }else{
                  $data = $data_no_pass;
              }              
              
              $this->db->where('AdminID',$AdminID);
              if($this->db->update('admin',$data)){
                  // Get Phan quyen
                  $this->db->where('AdminID',$AdminID);
                  $this->db->delete('permission_admin');
                  
                  $ar_id = $this->input->post('ar_id');
                  for($i=0;$i<sizeof($ar_id);$i++){
                      $data_permit = array(
                        'AdminID' => $AdminID,
                        'FunctionID' => $ar_id[$i]
                      );
                      $this->db->insert('permission_admin',$data_permit);
                  }
                  
                  return true;
              }else{
                  return False;
              }
          }else{
              $data = array(
                'LoginName' => $this->input->post('LoginName'),
                'Passwd' => md5($this->input->post('Passwd'))
              );              
              if($this->db->insert('admin',$data)){
                  $AdminID = $this->db->insert_id();
                  $ar_id = $this->input->post('ar_id');
                  
                  for($i=0;$i<sizeof($ar_id);$i++){
                      $data_permit = array(
                        'AdminID' => $AdminID,
                        'FunctionID' => $ar_id[$i]
                      );
                      $this->db->insert('permission_admin',$data_permit);
                  }                  
                  return true;
              }else{
                  return false;
              }
          }
      }
      
      function delete($id)
      {
            $this->db->where('AdminID',$id);
            $query = $this->db->delete('admin');
            if($query){
                return true;
            }else{
                return false;
            }
      }
	  
      function get_list_permit(){
          return $this->db->get('permission')->result();
      }
	  
      function get_list_func($PermissionID){
          $this->db->where('PermissionID',$PermissionID);
          return $this->db->get('permission_detail')->result();
      } 
      
      function get_item_permit_admin($id,$AdminID)
	  {
          $this->db->where('FunctionID',$id);
          $this->db->where('AdminID',$AdminID);
          $check = $this->db->get('permission_admin')->row();
          if($check){
              return true;
          }else{
              return false;
          }
      }     
}
?>
